<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\17 0017
 * Time: 9:21
 * 尝试构建 mvc 框架
 */

define('ROOT_DIR',__DIR__);
define('API_DIR',__DIR__ . '/' . 'api');
require_once ROOT_DIR . '/controller.php';
require_once ROOT_DIR . '/model.php';

require_once ROOT_DIR . '/library/utf8.php';

spl_autoload_register(function($controller_name){

    $controller_name = str_replace( "\\",'/',$controller_name);
    require_once ROOT_DIR . '/' . $controller_name . '.php';

});
$container = [];
$config = require_once ROOT_DIR . '/config.php';
require_once ROOT_DIR . '/db.php';

//todo 连接数据库
//todo 尝试 给 model 基类中插入db 做为静态属性， 以方便子类调用

model::setDB( new db( $config['db'] ) );
session_start();
//todo 设置路由，指向对应的控制器方法
$router_str = !empty($_GET['r']) ? $_GET['r']:'';

$router_split = explode('/',$router_str);
if( count($router_split) > 2 ){
    list($module, $controller, $method ) = $router_split;
}
else{

    if( is_dir(API_DIR . '/' . $router_split[0]) ){

        $module = $router_split[0];
        $controller = count($router_split) > 1 ? $router_split[1]:'index';
    }else if( is_file(API_DIR . '/' . $router_split[0] .'.php') ) {

        $controller = $router_split[0];
        $method = count($router_split) > 1 ? $router_split[1]:'index';
    }

}

$controller = empty($controller) ? 'index' : $controller;
$method = empty($method) ? 'index' : $method;

if(!empty($module) ){
    $controller_name = 'api\\' . $module.'\\' .$controller;
}else{
    $controller_name = 'api\\' . $controller;
}

$action = new $controller_name( $container );

//todo 判断方法是否存在，然后调用
$action($method);

















